package org.ala.tiktools.rpc.plugin.node;

/**
 * 用于唯一标识一个节点
 * @author ala
 * @date 2025-04-07 11:03
 */
public class NodeKey {
    /**
     *
     */
    protected String host;
    /**
     * 
     */
    protected int port;
    /**
     * 	NodeKey一旦创建不可改，提前把hashCode算出来。string的hashCode是O(n)
     */
    protected int hashCode;

    
    public static NodeKey build(String host, int port) {return new NodeKey(host, port);}
    private NodeKey(String host, int port) {
    	this.host = host;
    	this.port = port;
    	this.hashCode = host.hashCode() + port;
    }

    @Override
	public int hashCode() {
		return hashCode;
	}

	@Override
    public boolean equals(Object obj) {
        if (!(obj instanceof NodeKey)) {return false;}
        NodeKey nk = (NodeKey) obj;
        return this.host.equals(nk.host) && this.port == nk.port;
    }

    @Override
    public String toString() {
        return String.format("NodeKey{host:%s, port:%d}", host, port);
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

}
